package com.xjc.infra.encrypt;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.xjc.infra.exception.IFSException;
import com.xjc.infra.util.SystemGlobals;
/**
 * 密钥工厂
 * @author liangcz
 * @Date   2017年12月1日 下午7:19:58
 * @version 1.0
 */
@Component
public class KeyFactory {
	/**
	 * 鹏金所私钥
	 */
	private PrivateKey privateKey;
	/**
	 * 通联公钥
	 */
	private X509Certificate publicKey;
	/**
	 * 验签证书
	 */
	private X509Certificate cert;
	private boolean init = false;
	public void init(){
		try {
			String priKey = SystemGlobals.getProperty("http.PriKey"); // 鹏金所私钥
			String priKeyPassword = SystemGlobals.getProperty("http.PriKeyPassword"); // 私钥密码
			String pubKey = SystemGlobals.getProperty("http.PubKey"); // 通联证书
			/*priKey = "D:/keys/deposit/penging-123456.pfx";
			priKeyPassword = "123456";
			pubKey = "D:/keys/deposit/penging.crt";*/
			
			/*priKey = "D:/keys/fps-111111.pfx";
			priKeyPassword = "111111";
			pubKey = "D:/keys/fps.cer";*/
			
			Object[] obj = parseKeys(priKey,priKeyPassword);
			FileInputStream inStream = new FileInputStream(pubKey);
			privateKey = (PrivateKey) obj[1];
			publicKey = (X509Certificate) obj[0];
			CertificateFactory cf = CertificateFactory.getInstance("X.509");
		    cert = (X509Certificate)cf.generateCertificate(inStream);
		} catch (IFSException e) {
			throw e;
		} catch (Exception e) {
			throw new IFSException(e, "KeyFactory.init.99", "证书初始化失败");
		}
	}
	/**
	 * 解析证书
	 * @author liangcz
	 * @date   2017年12月1日 下午7:33:01
	 * @return Object[]
	 */
	private static Object[] parseKeys(String priKeyFileName, String priKeyPassword){
		try {
			KeyStore tKeyStore = KeyStore.getInstance("PKCS12");
			tKeyStore.load(new FileInputStream(priKeyFileName), priKeyPassword.toCharArray());
			@SuppressWarnings("rawtypes")
			Enumeration e = tKeyStore.aliases();
			String tAliases = "";
			while (e.hasMoreElements()) {
				tAliases = (String) e.nextElement();
			}
			X509Certificate tCert = (X509Certificate) tKeyStore.getCertificate(tAliases);
			PrivateKey tPrivateKey = (PrivateKey) tKeyStore.getKey(tAliases,priKeyPassword.toCharArray());
			Object[] tPfx = new Object[2];
			tPfx[0] = tCert;
			tPfx[1] = tPrivateKey;
			return tPfx;
		} catch (IFSException e) {
			throw e;
		} catch (Exception e) {
			throw new IFSException(e, "KeyFactory.parseKeys.99", "解析证书失败");
		}
	}
	
	
	public PrivateKey getPrivateKey() {
		if(init ==  false){
			init();
		}
		return privateKey;
	}
	public void setPrivateKey(PrivateKey privateKey) {
		this.privateKey = privateKey;
	}
	public X509Certificate getPublicKey() {
		if(init ==  false){
			init();
		}
		return publicKey;
	}
	public void setPublicKey(X509Certificate publicKey) {
		if(init ==  false){
			init();
		}
		this.publicKey = publicKey;
	}
	public X509Certificate getCert() {
		if(init ==  false){
			init();
		}
		return cert;
	}
	public void setCert(X509Certificate cert) {
		this.cert = cert;
	}
	
}
